package com.zzyl.nursing.controller;

import cn.hutool.json.JSONObject;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 智能设备Controller
 *
 * @author ruoyi
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "智能设备的接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('elder:device:list')")
    @GetMapping("/list")
    @ApiOperation("查询设备列表")
    public TableDataInfo list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }


    /**
     * 同步产品列表
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        return success(deviceService.allProduct());
    }

    /**
     * 注册设备
     *
     * @param dto 传输对象
     * @return 结果
     */
    @PostMapping("/register")
    @ApiOperation(value ="注册设备")
    public AjaxResult register(@RequestBody DeviceDto dto) {
        deviceService.register(dto);
        return success();
    }

    /***
     * 获取设备详细信息
     * @param dto 传输对象
     * @return 结果
     */
    @PostMapping("/queryDeviceDetail")
    @ApiOperation(value ="获取设备详细信息")
    public AjaxResult queryDeviceDetail(@RequestBody DeviceDto dto) {
        DeviceDetailVo vo = deviceService.queryDeviceDetail(dto.getIotId());
        return success(vo);
    }

    /**
     * 查看指定产品的已发布物模型中的功能定义详情
     *
     * @param dto 数据传输对象
     * @return 结果
     */
    @ApiOperation(value ="查看指定产品的已发布物模型中的功能定义详情")
    @PostMapping("/queryProductModelInfo")
    public AjaxResult queryProductModelInfo(@RequestBody DeviceDto dto) {
        JSONObject jsonStr = deviceService.queryProductModelInfo(dto.getProductKey());
        return success(jsonStr);
    }

    /**
     * 查询指定设备的物模型运行状态
     */
    @ApiOperation(value ="查询指定设备的物模型运行状态")
    @PostMapping("/queryDevicePropertyStatus")
    public AjaxResult queryDevicePropertyStatus(@RequestBody DeviceDto dto) {
        return deviceService.queryDevicePropertyStatus(dto.getDeviceName(), dto.getProductKey());
    }

    /**
     * 修改设备备注名称
     */
    @PutMapping
    public AjaxResult updateDevice(@RequestBody DeviceDto dto) {
        deviceService.updateDevice(dto);
        return success();
    }

    /**
     *  删除设备
     */
    @DeleteMapping
    public AjaxResult delete(String iotId) {
        return deviceService.delete(iotId);
    }
}